﻿using DotNetExtensions.Core.Logging;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNetExtensions.Core.SqlSugar
{
    public class BaseRepository<TModel> where TModel : class, new()
    {
        public readonly SqlSugarScope Db;
        private readonly ILogger<TModel> logger;

        public BaseRepository(string connectString)
        {
            logger = LoggerManager.GetLogger<TModel>();
            Db = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = DbType.MySql,
                ConnectionString = connectString,
                IsAutoCloseConnection = true
            });
        }
        public BaseRepository(string connectString, DbType dbType)
        {
            logger = LoggerManager.GetLogger<TModel>();
            Db = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = dbType,
                ConnectionString = connectString,
                IsAutoCloseConnection = true
            });
        }

        public BaseRepository(string connectString, DbType dbType, bool logging)
        {
            logger = LoggerManager.GetLogger<TModel>();
            Db = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = dbType,
                ConnectionString = connectString,
                IsAutoCloseConnection = true
            },
           db =>
           {
               if (logging)
               {
                   db.Aop.OnLogExecuted = (s, p) =>
                   {
                       logger.LogDebug($"p == null {p == null}");
                       string pv = p != null ? string.Join(",", p.Select(x => x.ParameterName + ":" + x.Value)) : "";
                       logger.LogDebug($"OnLogExecuted {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}: {s} {pv}");
                   };
               }
           });
        }

        public BaseRepository(RepositoryOptions options)
        {
            logger = LoggerManager.GetLogger<TModel>();
            Db = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = options.DbType,
                ConnectionString = options.ConnectionString,
                IsAutoCloseConnection = true
            },
           db =>
           {
               if (options.Logging)
               {
                   db.Aop.OnLogExecuted = (s, p) =>
                   {
                       logger.LogDebug($"p == null {p == null}");
                       string pv = p != null ? string.Join(",", p.Select(x => x.ParameterName + ":" + x.Value)) : "";
                       logger.LogDebug($"OnLogExecuted {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}: {s} {pv}");
                   };
               }
           });
        }


        public ISimpleClient<TModel> Model { get => Db.GetSimpleClient<TModel>(); }
        public ISimpleClient<T> NewModel<T>() where T : class, new()
        {
            return Db.GetSimpleClient<T>();
        }
    }
}
